package com.myProject.control.defence;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;

import com.myProject.bean.TArea;
import com.myProject.bean.TClass;
import com.myProject.bean.TMeet;
import com.myProject.bean.TThing;
import com.myProject.serves.Impl.ThingSevImpl;
import com.myProject.serves.ThingSev;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet(name = "ThingServlet", urlPatterns = "/thing")
public class ThingServlet extends HttpServlet {
    ThingSev ts = new ThingSevImpl();

    /**
     * @param request
     * @param response
     * @return void
     * @author liuyulai
     * @date 21.4.29 10:21
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    /**
     * @param request
     * @param response
     * @return void
     * @author liuyulai
     * @date 21.4.29 10:21
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String med = request.getParameter("method");
        if (med == null || "".equalsIgnoreCase(med)) {
            try {
                showAll(request, response);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else if ("add".equalsIgnoreCase(med)) {
            try {
                addThing(request, response);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else if ("alt".equalsIgnoreCase(med)) {
            altThing(request, response);
        } else if ("needHelp".equalsIgnoreCase(med)) {
            needHelp(request, response);
        }
    }

    private void needHelp(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter pw = response.getWriter();
        int pageNo = 1;
        String Page = request.getParameter("page");
        if (Page != null) {
            pageNo = Integer.parseInt(Page);
        }
        System.out.println("pageNo = " + pageNo);
        String limit = request.getParameter("limit");
        int pageSize = Integer.parseInt(limit);
        TThing tThing = new TThing();
        tThing.setState("需求专家");
        PageInfo<TThing> show = ts.helpByPage(pageNo, pageSize, tThing);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(show);
        System.out.println("json = " + json);
        pw.write(json);
    }

    private void addThing(HttpServletRequest request, HttpServletResponse response) throws ParseException {
        TThing tThing = new TThing();
        String thingName = request.getParameter("thingName");
        tThing.setThingName(thingName);
        String time = request.getParameter("time");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (time != null && !"".equalsIgnoreCase(time)) {
            tThing.setTime(sdf.parse(time));
        }
        int fkClassId = Integer.parseInt(request.getParameter("classId"));
        TClass tc = new TClass();
        tc.setPkId(fkClassId);
        tThing.settClass(tc);
        String method = request.getParameter("methods");
        tThing.setMethod(method);
        String state = request.getParameter("state");
        tThing.setState(state);
        String describe = request.getParameter("describe");
        tThing.setDescribes(describe);
        String type = request.getParameter("type");
        tThing.setType(type);
        String lose = request.getParameter("lose");
        tThing.setLose(lose);
        String found = request.getParameter("found");
        tThing.setFound(found);
        String size = request.getParameter("size");
        tThing.setSize(size);

        System.out.println("tThing = " + tThing);

        boolean flag = ts.addThing(tThing);
        System.out.println("flag = " + flag);
    }

    private void altThing(HttpServletRequest request, HttpServletResponse response) {
        String state = request.getParameter("state");
        String method = request.getParameter("methods");
        int pkId = Integer.parseInt(request.getParameter("pkId"));
        TThing tThing = new TThing();
        tThing.setPkId(pkId);
        tThing.setState(state);
        tThing.setMethod(method);
        System.out.println("tThing = " + tThing);
        boolean flag = ts.updateThing(tThing);
        System.out.println("flag = " + flag);
    }

    private void showAll(HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
        PrintWriter pw = response.getWriter();
        int pageNo = 1;
        String Page = request.getParameter("page");
        if (Page != null) {
            pageNo = Integer.parseInt(Page);
        }
        System.out.println("pageNo = " + pageNo);
        String limit = request.getParameter("limit");
        int pageSize = Integer.parseInt(limit);

        String thingName = request.getParameter("thingName");
        String area = request.getParameter("area");
        String state = request.getParameter("state");
        String time1 = request.getParameter("date1");
        String time2 = request.getParameter("date2");

        TClass tc = new TClass();
        tc.setArea(area);
        TThing tThing = new TThing();
        tThing.setThingName(thingName);
        tThing.setState(state);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (time1 != null && !"".equalsIgnoreCase(time1)) {
            tThing.setStartTime(sdf.parse(time1));
        }

        if (time2 != null && !"".equalsIgnoreCase(time2)) {
            tThing.setEndTime(sdf.parse(time2));
        }
        tThing.settClass(tc);
        System.out.println("tThing = " + tThing);
        PageInfo<TThing> show = ts.showThingByPage(pageNo, pageSize, tThing);

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(show);
        System.out.println("json = " + json);
        pw.write(json);

    }
}
